package com.browsesoft.baseadapter;

import java.util.LinkedList;

import javax.servlet.http.HttpServletRequest;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.browsesoft.EntityManager;
import com.browsesoft.ExtendElement;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.resource.BasicResource;
import com.browsesoft.resource.ResourceTool;
import com.browsesoft.resource.RightType;
import com.browsesoft.user.User;

/**
 * 可以根据权限进行处理的简单资源列表model
 */
public class HTMLRightTableModel extends HTMLBasicTableModel {
	/**
	 * 用户有无权限
	 */
	protected boolean hasRight = false;

	/**
	 * 没有权限返回的功能列数
	 */
	protected int noRightCount = 0;

	/**
	 * 有权限返回的功能列数
	 */
	protected int hasRightCount = 0;

	/**
	 * 初始化model
	 * 
	 * @param request
	 *            用户请求
	 * @throws Exception
	 */
	public void init(HttpServletRequest request) throws Exception {
		super.init(request);
		// 得到登陆用户
		User user = (User) request.getSession().getAttribute("loginUser");
		// 得到要看的资源
		BasicResource res = (BasicResource) EntityManager.getInstance()
				.getObject(this.parentID);
		// 用户是否有权限
		this.hasRight = ResourceTool.hasRight(user, res, new RightType("编辑"));
		// 得到组件元素中的form属性值，用来
		Element formElement = (Element) this.component.getElement();
		// 得到实体类型
		String rightArea = formElement.getAttribute("rightarea");
		if (rightArea != null && !rightArea.equals("")) {
			try {
				this.noRightCount = Integer.parseInt(rightArea.substring(0,
						rightArea.indexOf("-")));
				this.hasRightCount = Integer.parseInt(rightArea
						.substring(rightArea.indexOf("-") + 1));
			} catch (Exception e) {
				System.out.println("rightarea 的属性值错误，导致不能正常显示！");
			}
		}
	}

	/**
	 * 修改单元值
	 * 
	 * @param td
	 *            td
	 * @param doc
	 *            doc
	 * @param row
	 *            row
	 * @param col
	 *            col
	 */
	public void modifyCellValue(Element td, Document doc, int row, int col) {
		try {
			com.browsesoft.Entity entity = (com.browsesoft.Entity) datas
					.get(row);
			// 显示名字
			if (col < cnames.size()) {
				// 得到链接
				LinkedList nl = ExtendElement.findElements(td, "type", "link");
				// 如果有链接
				if (nl.size() != 0) {
					// 换id号
					Element link = (Element) nl.get(0);
					String value = link.getAttribute("onclick");
					String id = (String) entity.getAttributes().get("id");
					value = value.replaceAll("#nodeid#", id);
					link.setAttribute("onclick", value);
				}
				// 替换要显示的内容
				String ename = (String) enames.get(col);
				String showContent = (String) entity.getAttributes().get(ename);
				if (showContent == null) {
					showContent = "　";
				}
				ExtendElement.replaceElementContext(td, doc
						.createTextNode(showContent));
			}
			// 没有权限的功能列数
			else if (col < cnames.size() + noRightCount) {
				// 替换td中属性值
				this.replaceTD(td, entity);
			}
			// 有权限的功能列数
			else if (col < cnames.size() + hasRightCount) {
				// 如果登陆用户对给定资源有编辑权限
				if (this.hasRight) {
					// 替换td中属性值
					this.replaceTD(td, entity);
				}
				// 否则没有权限
				else {
					// 删除自己
					td.getParentNode().removeChild(td);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 模型更新页面元素
	 * 
	 * @param c
	 *            基本组件
	 * @throws Exception
	 */
	public void updatePageElement(HTMLBasicComponent c) throws Exception {
		Element body = (Element) component.getPage().getDocument()
				.getElementsByTagName("body").item(0);
		LinkedList ls = ExtendElement
				.findElements(body, "rightadd", "rightadd");
		if (ls.size() > 0) {
			if (!this.hasRight) {
				Element add = (Element) ls.get(0);
				add.getParentNode().removeChild(add);
			}
		}
	}
}
